/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.inpatient.service.impl;

import me.zhengjie.modules.his.domain.HospitalPrice;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.inpatient.repository.HospitalPriceRepository;
import me.zhengjie.modules.his.inpatient.service.HospitalPriceService;
import me.zhengjie.modules.his.inpatient.service.dto.HospitalPriceDto;
import me.zhengjie.modules.his.inpatient.service.dto.HospitalPriceQueryCriteria;
import me.zhengjie.modules.his.inpatient.service.mapstruct.HospitalPriceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class HospitalPriceServiceImpl implements HospitalPriceService {

    private final HospitalPriceRepository hospitalPriceRepository;
    private final HospitalPriceMapper hospitalPriceMapper;

    @Override
    public Map<String,Object> queryAll(HospitalPriceQueryCriteria criteria, Pageable pageable){
        Page<HospitalPrice> page = hospitalPriceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(hospitalPriceMapper::toDto));
    }

    @Override
    public List<HospitalPriceDto> queryAll(HospitalPriceQueryCriteria criteria){
        return hospitalPriceMapper.toDto(hospitalPriceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public HospitalPriceDto findById(Integer hospitalPriceId) {
        HospitalPrice hospitalPrice = hospitalPriceRepository.findById(hospitalPriceId).orElseGet(HospitalPrice::new);
        ValidationUtil.isNull(hospitalPrice.getHospitalPriceId(),"HospitalPrice","hospitalPriceId",hospitalPriceId);
        return hospitalPriceMapper.toDto(hospitalPrice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HospitalPriceDto create(HospitalPrice resources) {
        return hospitalPriceMapper.toDto(hospitalPriceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(HospitalPrice resources) {
        HospitalPrice hospitalPrice = hospitalPriceRepository.findById(resources.getHospitalPriceId()).orElseGet(HospitalPrice::new);
        ValidationUtil.isNull( hospitalPrice.getHospitalPriceId(),"HospitalPrice","id",resources.getHospitalPriceId());
        hospitalPrice.copy(resources);
        hospitalPriceRepository.save(hospitalPrice);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer hospitalPriceId : ids) {
            hospitalPriceRepository.deleteById(hospitalPriceId);
        }
    }

    @Override
    public void download(List<HospitalPriceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HospitalPriceDto hospitalPrice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("住院", hospitalPrice.getRegisterId());
            map.put("药品/项目名称", hospitalPrice.getDrugName());
            map.put("药品/项目数量", hospitalPrice.getDrugNum());
            map.put("价钱", hospitalPrice.getRepicePrice());
            map.put("小计", hospitalPrice.getRepiceTotal());
            map.put(" hTime",  hospitalPrice.getHTime());
            map.put(" state",  hospitalPrice.getState());
            map.put("创建者", hospitalPrice.getCreateBy());
            map.put("更新者", hospitalPrice.getUpdateBy());
            map.put("创建日期", hospitalPrice.getCreateTime());
            map.put("更新时间", hospitalPrice.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}